<!DOCTYPE html>
<script src="../node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>
<script src="../node_modules/web-component-tester/browser.js"></script>

<script type="module" src="../src/tricium-run.js"></script>

<test-fixture id="element">
  <template>
    <tricium-run run="1234"></tricium-run>
  </template>
</test-fixture>

<script>

suite('<tricium-run>', () => {
  let element;

  setup(() => {
    sinon.stub(window, 'fetch');
    window.fetch.returns(Promise.resolve());
    element = fixture('element');
  });

  teardown(() => {
    window.fetch.restore();
  });

  test('properties', () => {
    assert.equal(element.run, '1234');
    assert.isUndefined(element.data);
    assert.isUndefined(element.error);
  });

  test('_refresh updates data', async () => {
    const data = {
      'runId': '12345',
      'state': 'SUCCESS',
      'functionProgress': [
        {
          'name': 'Spacey',
          'state': 'SUCCESS',
          'swarmingUrl': 'https://chromium-swarm.appspot.com',
          'swarmingTaskId': '3e2d03fcebd87610',
          'numComments': 1,
        },
      ],
    };
    window.fetch.returns(Promise.resolve(newSuccessResponse(data)));
    await element._refresh();
    console.log(element.data);
    assert.deepEqual(data, element.data);
  });

  test('_renderLink given function progress with buildbucket build', () => {
    const f = {
      'name': 'RecipeNoOp',
      'state': 'SUCCESS',
      'buildbucketHost': 'cr-buildbucket-dev.appspot.com',
      'buildbucketBuildId': '1234567890',
    };
    // Below we compare the filled-in template values in order to avoid
    // having to actually render the lit-html TemplateResult that is
    // returned by _renderLink. This might also make the test slightly
    // more resilient to minor template changes, but it doesn't test how
    // the rendered HTML will actually look.
    assert.deepEqual(
        element._renderLink(f).values,
        ['luci-milo-dev.appspot.com', '1234567890', '1234567890']);
  });

  test('_renderLink given function progress with swarming task', () => {
    const f = {
      'name': 'RecipeNoOp',
      'state': 'SUCCESS',
      'swarmingUrl': 'https://chromium-swarm.appspot.com',
      'swarmingTaskId': 'deadbeef0',
    };
    assert.deepEqual(
        element._renderLink(f).values,
        ['https://chromium-swarm.appspot.com', 'deadbeef0', 'deadbeef0']);
  });

  function newSuccessResponse(data) {
    return new window.Response(')]}\'' + JSON.stringify(data), {
      status: 200,
      headers: {
        'Content-Type': 'application/json',
        'X-Prpc-Grpc-Code': '0',
      },
    });
  }
});
</script>
